package com.yxhl.zoume.core.user.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.yxhl.zoume.R;
import com.yxhl.zoume.account.event.UserLoginEvent;
import com.yxhl.zoume.common.ui.activity.base.BaseActivityWithToolBar;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.user.presenter.IdentificationPresenter;
import com.yxhl.zoume.core.user.ui.fragment.identification.IdentificationCheckFragment;
import com.yxhl.zoume.core.user.ui.fragment.identification.IdentificationDoneFragment;
import com.yxhl.zoume.core.user.ui.fragment.identification.IdentificationErrorFragment;
import com.yxhl.zoume.core.user.ui.fragment.identification.IdentificationPrepareFragment;
import com.yxhl.zoume.core.user.view.IdentificationView;
import com.yxhl.zoume.data.http.rest.param.identification.IdentificationParam;
import com.yxhl.zoume.data.http.rest.response.identification.IdentificationResponse;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.di.component.usercenter.DaggerIdentificationComponent;
import com.yxhl.zoume.di.module.ActivityModule;
import com.yxhl.zoume.utils.AccountUtils;
import com.yxhl.zoume.utils.LOG;
import com.yxhl.zoume.utils.PromptUtils;

import javax.inject.Inject;

import butterknife.BindView;
import rx.functions.Action1;

public class NameIdentificationActivity extends BaseActivityWithToolBar implements IdentificationView, BaseView {


    private static final String TAG = "NameIdentificationAct";
    private static final int SUCCESS = 1;
    private static final int FAILURE = 2;
    private static final int ERROR = 3;
    @BindView(R.id.frame_activity_name_identification_content)
    FrameLayout mFrameLayout;
    @BindView(R.id.pb_loading)
    ProgressBar mPbLoading;
    @Inject
    IdentificationPresenter mIdentificationPresenter;
    private String mMoblie;
    private String mToken;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mPbLoading.setVisibility(View.GONE);
            switch (msg.what) {
                case SUCCESS: {
                    IdentificationResponse response = (IdentificationResponse) msg.obj;
                    // authStatus：认证状态  (0:未认证  1:已通过   2:未通过   3:审核中)
                    switch (response.getAuthStatus()) {
                        case 0:
                            replaceFragment(R.id.frame_activity_name_identification_content, IdentificationPrepareFragment.newInstance(response), false);
                            break;
                        case 1:
                            replaceFragment(R.id.frame_activity_name_identification_content, IdentificationDoneFragment.newInstance(response), false);
                            break;
                        case 2:
                            replaceFragment(R.id.frame_activity_name_identification_content, IdentificationErrorFragment.newInstance(response), false);
                            break;
                        case 3:
                            replaceFragment(R.id.frame_activity_name_identification_content, IdentificationCheckFragment.newInstance(response), false);
                            break;
                    }
                }
                break;
                case FAILURE:
                    String message = (String) msg.obj;
                    PromptUtils.showShort(mBaseContext, message);
                    break;
                case ERROR:
                    PromptUtils.showShort(mBaseContext, R.string.net_error);
                    break;
            }
        }
    };


    public static Intent getCallingIntent(Activity activity) {
        Intent intent = new Intent(activity, NameIdentificationActivity.class);
        return intent;
    }

    public void onRxBusEvent() {
        RxBus.getInstance().toSubscription(UserLoginEvent.class, new Action1<UserLoginEvent>() {
            @Override
            public void call(UserLoginEvent userLoginEvent) {
                finish();
            }
        });


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mIdentificationPresenter.attachView(this);


    }


    @Override
    protected void onStart() {
        super.onStart();
        onRxBusEvent();
        queryIDStatus();
    }

    private void queryIDStatus() {
        IdentificationParam param = new IdentificationParam();
        mMoblie = AccountUtils.getInstance(this).getAccount().getAccountName();
        mToken = AccountUtils.getInstance(this).getAccount().getAccessToken();
        if (TextUtils.isEmpty(mMoblie) || TextUtils.isEmpty(mToken)) {
            navigator.navigateToMobileLogin(this);
            PromptUtils.showShort(mBaseContext, R.string.login_dialog_content);
            finish();
        } else {
            param.setMobile(mMoblie)
                    .setToken(mToken);
            mIdentificationPresenter.QueryIDStatus(param);
        }
    }

    public void postIdCertify(IdentificationParam param) {
        mIdentificationPresenter.postIdCertify(param);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onHeaderLeftClick(View view) {
        onBackPressed();
    }

    @Override
    protected void onHeaderRightClick(View view) {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_name_identification;
    }

    @Override
    protected void initializeInjector() {
        DaggerIdentificationComponent.builder()
                .activityModule(new ActivityModule(this))
                .appComponent(getAppComponent())
                .build()
                .inject(this);


    }

    @Override
    public void renderIDSuccess(IdentificationResponse param) {
        Message message = mHandler.obtainMessage(SUCCESS);
        message.obj = param;
        mHandler.sendMessage(message);

    }

    @Override
    public void renderIDFailure(String resultMessage) {
        Message message = mHandler.obtainMessage(FAILURE);
        message.obj = resultMessage;
        mHandler.sendMessage(message);

    }

    @Override
    public void renderIDError(Throwable e) {
        Message message = mHandler.obtainMessage(ERROR);
        LOG.e(TAG, e.getMessage());
        mHandler.sendMessage(message);
    }
}
